const baseUrl = "http://localhost:9090/api";

export class WebRequest {
  static fetchData(method, url, data) {
    const token = localStorage.getItem("token");
    url = baseUrl + url;
    return new Promise((resolve, reject) => {
      fetch(url, {
        method: method, // *GET, POST, PUT, DELETE, etc.
        cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
        mode:"cors",
        headers: {
          "Content-Type": "application/json",
          token,
        },
        // redirect: "follow", // manual, *follow, error
        // referrerPolicy: "no-referrer", // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
        body: JSON.stringify(data), // body data type must match "Content-Type" header
      })
        .then((res) => {
          return res.json();
        })
        .then((res) => {
          const { code, data, msg } = res;
          if (code === 0) {
            resolve(data);
          } else {
            debugger;
            if (msg) {
              console.error(msg);
            }
            reject(res);
          }
        });
    });
  }
  static get(...args) {
    return this.fetchData("GET", ...args);
  }

  static post(...args) {
    return this.fetchData("POST", ...args);
  }
}

// const webRequest = {
//     get, post
// }

// module.exports = {
//     get, post
// }
// export default webRequest;
